package edu.umb.cs.antmanager.common;

import java.io.File;

/**
 * Common constants used by Agent and Manager.
 * 
 * @author 2009CS682-3UMB_AntManager
 * 
 */
public final class Constants {
	/**
	 * Agent's status idle
	 */
	public static final String STATUS_IDLE = "idle";
	/**
	 * Agent is busy
	 */
	public static final String STATUS_BUSY = "busy";

	// Task types for manger.AgentConnRunnable
	/**
	 * task for get agent's status
	 */
	public static final int TASK_TYPE_STATUS = 1;
	/**
	 * task for execute test on agent
	 */
	public static final int TASK_TYPE_EXECUTE = 2;
	/**
	 * task for shutdown an agent
	 */
	public static final int TASK_TYPE_SHUTDOWN = 3;
	/**
	 * task for restart an agent
	 */
	public static final int TASK_TYPE_RESTART = 4;

	public static final int TASK_TYPE_LOG = 5;

	// The home directory of the ant manager
	/**
	 * Home directory of System.
	 */
	public static final String ANT_MANAGER_HOME = getAntManagerHome();

	/**
	 * Port number of the agent
	 */
	public static final int AGENT_PORT = 1099;

	/**
	 * Port number of the manager for agent to call back
	 */
	public static final int MANAGER_CALLBACK_PORT = 2099;
	/**
	 * Agent's service name in the registry
	 */
	public static final String AGENT_SERVER_NAME = "AntAgent";

	public static final int CONTROL_PORT = 1199;
	public static final String AGENT_CONTROLLER = "AgentController";
	/**
	 * Code base for RMI to locate remote object
	 */
	public static final String CODE_BASE = getCodeBase();
	/**
	 * The file contains the security policy
	 */
	public static final String SECURITY_FILE = ANT_MANAGER_HOME
			+ "security.policy";

	public static final String ANTMANAGER_TEST_CONTINUE = "antmanager.test.continue";

	/**
	 * Get the home directory of ant manager if environment variable
	 * ANT_MANAGER_HOME is not set, use current directory (for running in
	 * eclipse)
	 * 
	 * @return The home directory of ant manager
	 */
	private static String getAntManagerHome() {
		String home = System.getenv("ANT_MANAGER_HOME");
		if (home == null)
			home = new File("").getAbsolutePath();
		return home + File.separator;
	}

	/**
	 * Get the CODE_BASE if folder build/classes exist, use it as code
	 * base(development environment) otherwise the jar file will be used
	 * 
	 * @return
	 */
	private static String getCodeBase() {
		File build = new File("build/classes");
		File jar = new File(ANT_MANAGER_HOME + "lib/ant-manager.jar");
		if (build.exists())
			return build.toURI().toString();
		return jar.toURI().toString();
	}
}
